package com.m3958.logcollector.datacommons.util;

import java.util.Map;
import java.util.Optional;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;

public class AllParams {
    
    private Map<String, String[]> originParams;
    
    
    public static AllParams createEmpty() {
        return new AllParams();
    }
    
    public AllParams() {
        this.setOriginParams(Maps.newHashMap());
    }
    
    public AllParams(Map<String, String[]> params) {
        this.setOriginParams(params);
    }
    
    /**
     * 对于String来说，空白和没有参数有一定的差别。
     * @param pn
     * @return
     */
    public Optional<String> getFirstValue(String pn) {
        if (!originParams.containsKey(pn)) {
            return Optional.empty();
        }
        String[] ss = originParams.get(pn);
        if (ss.length == 0) {
            return Optional.of("");
        } else {
            return Optional.of(ss[0]);
        }
    }
    
    /**
     * 对于id来说，空白的参数或者错误的参数相当于不存在。
     * @param pn
     * @return
     */
    public Optional<Long> getIdValue(String pn) {
        Optional<String> spv = getFirstValue(pn);
        if (spv.isPresent()) {
            String s = spv.get().trim();
            
            if (Strings.isNullOrEmpty(s)) {
                return Optional.empty();
            }
            try {
                return Optional.of(Long.valueOf(s));
            } catch (NumberFormatException e) {
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    }

    public Map<String, String[]> getOriginParams() {
        return originParams;
    }

    public void setOriginParams(Map<String, String[]> originParams) {
        this.originParams = originParams;
    }
}
